import Mock from 'mockjs'

const users = {
  admin: {
    password: 'admin',
    menu: [
      {path: '/home', name: 'home', label: '首页', icon: 'HomeOutlined'},
      {path: '/mall', name: 'mall', label: '商品管理', icon: 'ShopOutlined'},
      {path: '/mall1', name: 'mall1', label: '实验区1', icon: 'ShopOutlined'},
      {path: '/mall2', name: 'mall2', label: '实验区2', icon: 'ShopOutlined'},
      {path: '/mall3', name: 'mall3', label: '实验区3', icon: 'ShopOutlined'},
      {path: '/user', name: 'user', label: '用户管理', icon: 'UserOutlined'},
      {path: '/other', label: '其他', icon: 'SettingOutlined', children: [
        {path: '/other/pageOne', name: 'page1', label: '页面1', icon: 'SettingOutlined'},
        {path: '/other/pageTwo', name: 'page2', label: '页面2', icon: 'SettingOutlined'},
      ]}
    ]
  },
  user: {
    password: '123',
    menu: [
      {path: '/home', name: 'home', label: '首页', icon: 'HomeOutlined'},
      {path: '/mall', name: 'mall', label: '商品管理', icon: 'ShopOutlined'},
    ]
  }
};

export default {
  getMenu: config => {
    const { username, password } = JSON.parse(config.body)

    // 先判断用户是否存在
    // users[username] 是对象的动态属性访问，可以通过用户名直接从对象中获取对应信息
    const user = users[username]
    if(!user) {
      return { code: -999, message: '用户不存在' }
    }
    
    // 判断账号和密码是否对应
    if ( user.password !== password) {
      return { code: -999, message: '密码错误' }
    }

    return { 
      code: 200,
      data: {
        menu: user.menu,
        token: Mock.Random.guid(),
        message: '登录成功'
      } 
    }
  }
}